package EurekaProcessor;


import Config.GlobalLog;
import EurekaAnalyzer.IDInformation;

public class IDExtractor extends Thread {
	private ProcessorMgr pMgr;
	
	public IDExtractor(ProcessorMgr pMgr)
	{
		this.pMgr = pMgr;
	}
	/*
	@Deprecated
	private PipelineMgr pMgr;
	
	@Deprecated
	public IDExtractor(PipelineMgr pMgr)
	{
		this.pMgr = pMgr;
	}
	*/
	
	
	void ExtractIDInfoFromRawData(RawDataFromPage rd)
	{
		int size = rd.ids.size();
		if(size==0)
			return;
		if(rd.ids.size()!=rd.times.size())
			return;
		for(int i=0;i<size;i++)
		{
			IDInformation idInfo = pMgr.GetInformationByID(rd.ids.get(i));
			idInfo.AppendBoard(rd.board);
			
			if (i == 0)
				idInfo.AppendPost(rd.times.get(i), rd.board);
			else
				idInfo.AppendReply(rd.times.get(i), rd.board);
			
			pMgr.PutIDInfo(rd.ids.get(i), idInfo);
		}
		//Now we append friends...
		IDInformation idInfo = pMgr.GetInformationByID(rd.ids.get(0));
		for(int i=1;i<size;i++)
		{
			if (!rd.ids.get(0).equals(rd.ids.get(i)))
				idInfo.AppendFriend(rd.ids.get(i));
		}
		pMgr.PutIDInfo(rd.ids.get(0), idInfo);
		
		rd.board = null;
		rd.ids.clear();
		rd.times.clear();
		rd = null;
	}
	
	public void run()
	{
		GlobalLog.AddLog("IDExtractor starts...");
		while(true)
		{
			if(pMgr.IsRawDataCollectionEmpty())
			{
				if(pMgr.CanExtractorQuit())
				{
					GlobalLog.AddLog("IDExtractor quits now...");
					pMgr.ExtractorFinished();
					return;
				}
				GlobalLog.AddLog("IDExtractor waits...");
				pMgr.WaitForRawData();
			}
			ExtractIDInfoFromRawData(pMgr.GetRawData());
		}
	}
}
